<?php

namespace App\Admin\Renderable;

use App\Admin\Repositories\SystemRegion;
use App\Models\VillagePublish;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;

class VillagePublishTable extends LazyRenderable
{
    public function grid(): Grid
    {
        $regionId = $this->payload['region_id'];

        return Grid::make(new VillagePublish(), function (Grid $grid) use ($regionId) {
            if ($regionId) {
                $grid->model()->whereIn('region_id', [0, $regionId]);
            }
            $grid->model()->where('is_show', 1);
            $grid->model()->where('check_status', 2);
            $grid->column('id', 'ID')->sortable();
            $grid->column('pub_title', '内容名称');
            $grid->column('list_img', '图片')->image('', 60, 60);
            $grid->column('region_id', '地区')->display(function ($region_id) {
                if ($region_id) {
                    $region = \App\Models\SystemRegion::find($region_id);
                    return $region ? $region->region_name : '';
                } else {
                    return '';
                }
            });
            $grid->column('created_at');

            $grid->quickSearch(['id', 'pub_title']);

            $grid->paginate(10);
            $grid->disableActions();

            $grid->selector(function (Grid\Tools\Selector $selector) {
                $regionList = SystemRegion::getList();
                $selector->selectOne('region_id', '地区筛选', $regionList);
            });
        });

    }
}